import json


class User:
    # 已注册的用户 用户名：(密码， 角色)
    user_p = {
        'tom': ("123", "teacher"),
        'jarry': ("123", "student")
    }

    error_count = {}

    @classmethod
    def login(cls, name, passwd):
        if cls.user_p.get(name) is None:
            print("user {} is not exist".format(name))
        else:
            if name in cls.error_count and cls.error_count[name] == 2:
                print("deny login")
                return

            if cls.user_p[name][0] == passwd:
                print("login success")
                return cls.user_p[name][1]
            else:
                print("passwd is wrong")
                cls.error_count[name] = cls.error_count.get(name, 0) + 1


class Search:
    def __init__(self):
        self.file = open('achieve.txt', mode="r+", encoding="utf-8")
        self.achieve_dict = json.load(self.file)

    def writer(self, st_name, chinese, math, language):
        self.achieve_dict.update({st_name: {"chinese": chinese, "math": math, "language": language}})
        self.file.seek(0)
        json.dump(self.achieve_dict, self.file)
        self.file.flush()

    def reader(self, name):
        return self.achieve_dict.get(name)

    def __del__(self):
        self.file.close()


search_input = Search()

while True:
    input_name = input("登录用户名>>>:")
    input_passwd = input("密码>>>:")
    role = User.login(input_name, input_passwd)
    if role == "teacher":
        while True:
            try:
                index = int(input("输入还是查询的成绩>>>:\n1.输入;\n2.查询;\n3.退出;\n请输入序号："))
            except ValueError:
                index = 999
            if int(index) == 1:
                student_name = input("输入学生姓名：")
                chinese = input("语文成绩：")
                math = input("数学成绩：")
                language = input("英语成绩：")
                search_input.writer(student_name, chinese, math, language)
            elif int(index) == 2:
                student_name = input("输入查询成绩的学生姓名：")
                res = search_input.reader(student_name)
                if res:
                    print("语文:{}, 数学:{}, 英语:{}".format(res["chinese"], res["math"], res["language"]))
                else:
                    print("你输入的学生还没有成绩！")
            elif int(index) == 3:
                break
            else:
                print("输入有误，请重新输入！")

    elif role == "student":
        while True:
            try:
                index = int(input("输入还是查询的成绩>>>:\n1.查询;\n2.退出;\n请输入序号："))
            except ValueError:
                index = 999
            if int(index) == 1:
                student_name = input("输入查询成绩的学生姓名：")
                res = search_input.reader(student_name)
                if res:
                    print("语文:{}, 数学:{}, 英语:{}".format(res["chinese"], res["math"], res["language"]))
                else:
                    print("你输入的学生还没有成绩！")
            elif int(index) == 2:
                break
            else:
                print("输入有误，请重新输入！")

# 这次实现的非常好，就是最后没有提供退出的流程。可以稍微再改一下。